﻿#include "Tools.h"
#include "ui_Tools.h"
// add by liq 12
#include <QToolTip>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QStyle>
//#include "CToolTip.h"
// add by liq test
#include <QMessageBox>

Tools::Tools(QWidget *parent) :
    YToolWidget(parent),
    ui(new Ui::Tools)
{
    setWindowFlags(Qt::ToolTip | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
//    setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_AlwaysShowToolTips);

    ui->setupUi(this);

    // add by liq 12
    // 开启鼠标实时追踪
//    setMouseTracking(true);

//    if(!g_toolTip){
//        g_toolTip = new CToolTip(this);
//    }

//    ui->radioButton_1->installEventFilter(this);
//    ui->radioButton_2->installEventFilter(this);
//    ui->radioButton_3->installEventFilter(this);
//    ui->radioButton_4->installEventFilter(this);
//    ui->radioButton_5->installEventFilter(this);
//    ui->radioButton_6->installEventFilter(this);
//    ui->radioButton_7->installEventFilter(this);
    ui->gridLayout_2->installEventFilter(this);
    ui->horizontalLayout_2->installEventFilter(this);
    ui->horizontalLayout_3->installEventFilter(this);
    ui->radioBox->installEventFilter(this);
    ui->split1->installEventFilter(this);

    ui->radioButton_1->setText(QChar(0xf24d));
    ui->radioButton_2->setText(QChar(0xf1db));
//    ui->radioButton_3->setText(QChar(0xf178));
    ui->radioButton_3->setText(QChar(0xf0ec));
    ui->radioButton_4->setText(QChar(0xf040));
//    ui->radioButton_4->setText(QChar(0xf1fc));
    ui->radioButton_5->setText(QChar(0xf1a5));
//    ui->radioButton_6->setText("A");
    ui->radioButton_6->setText(QChar(0xf031));
    ui->radioButton_7->setText(QChar(0xf25d));

    ui->radioButton_1->setToolTip(QString::fromLocal8Bit("矩形"));
    ui->radioButton_1->setToolTipDuration(3000);
    ui->radioButton_2->setToolTip(QString::fromLocal8Bit("椭圆"));
    ui->radioButton_2->setToolTipDuration(3000);
    ui->radioButton_3->setToolTip(QString::fromLocal8Bit("箭头"));
    ui->radioButton_3->setToolTipDuration(3000);
    ui->radioButton_4->setToolTip(QString::fromLocal8Bit("画笔"));
    ui->radioButton_4->setToolTipDuration(3000);
    ui->radioButton_5->setToolTip(QString::fromLocal8Bit("马赛克"));
    ui->radioButton_5->setToolTipDuration(3000);
    ui->radioButton_6->setToolTip(QString::fromLocal8Bit("文字"));
    ui->radioButton_6->setToolTipDuration(3000);
    ui->radioButton_7->setToolTip(QString::fromLocal8Bit("标签"));
    ui->radioButton_7->setToolTipDuration(3000);

    ui->pushButton_1->setText(QChar(0xf0e2));//undo
//    ui->pushButton_2->setText(QChar(0xf0c4));
    ui->pushButton_2->setText(QChar(0xf01e));//redo
    ui->pushButton_3->setText(QChar(0xf1ab));
    ui->pushButton_4->setText(QChar(0xf2b1));
//    ui->pushButton_5->setText(QChar(0xf1fb));//钉在桌面
    ui->pushButton_5->setText(QChar(0xf08d));//钉在桌面
    ui->pushButton_6->setText(QChar(0xf192));//录制
    ui->pushButton_7->setText(QChar(0xf019));//下载
//    ui->pushButton_7->setText(QChar(0xf0ed));//下载
    ui->pushButton_8->setText(QChar(0xf10b));
    ui->pushButton_9->setText(QChar(0xf02b));
    ui->pushButton_10->setText(QChar(0xf00d));//取消
    ui->pushButton_11->setText(QString(QChar(0xf00c)) + QString::fromLocal8Bit("完成"));//完成
//    ui->pushButton_11->setText(QString(QChar(0xf0c7)) + QString::fromLocal8Bit(" 完成"));//完成

    ui->pushButton_1->setToolTip(QStringLiteral("撤销"));
    ui->pushButton_1->setToolTipDuration(3000);
    onUpdateUndoBtnState(false);
    ui->pushButton_2->setToolTip(QStringLiteral("重做"));
    ui->pushButton_2->setToolTipDuration(3000);
    onUpdateRedoBtnState(false);
    ui->pushButton_5->setToolTip(QStringLiteral("钉在桌面"));
    ui->pushButton_5->setToolTipDuration(3000);
    ui->pushButton_6->setToolTip(QStringLiteral("录屏"));
    ui->pushButton_6->setToolTipDuration(3000);
    ui->pushButton_7->setToolTip(QStringLiteral("下载"));
    ui->pushButton_7->setToolTipDuration(3000);
    ui->pushButton_10->setToolTip(QStringLiteral("退出"));
    ui->pushButton_10->setToolTipDuration(3000);
    ui->pushButton_11->setToolTip(QStringLiteral("完成截图"));
    ui->pushButton_11->setToolTipDuration(3000);

    connect(ui->radioButton_1, &QRadioButton::toggled, [this]{
        if(ui->radioButton_1->isChecked()){
            emit draw(1);
        }
    });
    connect(ui->radioButton_2, &QRadioButton::toggled, [this]{
        if(ui->radioButton_2->isChecked()){
            emit draw(2);
        }
    });
    connect(ui->radioButton_3, &QRadioButton::toggled, [this]{
        if(ui->radioButton_3->isChecked()){
            emit draw(3);
        }
    });
    connect(ui->radioButton_4, &QRadioButton::toggled, [this]{
        if(ui->radioButton_4->isChecked()){
            emit draw(4);
        }
    });
    connect(ui->radioButton_5, &QRadioButton::toggled, [this]{
        if(ui->radioButton_5->isChecked()){
            emit draw(5);
        }
    });
    connect(ui->radioButton_6, &QRadioButton::toggled, [this]{
        if(ui->radioButton_6->isChecked()){
            emit draw(6);
        }
    });
    connect(ui->radioButton_7, &QRadioButton::toggled, [this]{
        if(ui->radioButton_7->isChecked()){
            emit draw(7);
        }
    });

    connect(ui->pushButton_1, &QPushButton::clicked, this, &Tools::undo);
    connect(ui->pushButton_2, &QPushButton::clicked, this, &Tools::redo);
    connect(ui->pushButton_5, &QPushButton::clicked, [this]{
        emit tack();
        emit finished();
    });
    connect(ui->pushButton_6, &QPushButton::clicked, [this]{
        emit record();
        emit finished();
    });
    connect(ui->pushButton_7, &QPushButton::clicked, [this]{
        emit download();
        // del by liq
        // 此处不要自动关闭截图，保存成功再关闭，取消保存时不关闭
//        emit finished();
    });
    connect(ui->pushButton_10, &QPushButton::clicked, [this]{
        // add by liq 14
        emit sigScreenshotCancel();
        emit finished();
    });
    connect(ui->pushButton_11, &QPushButton::clicked, [this]{
        emit clipBoard();
        // add by liq 14
        emit sigSaveImageToClipboard();
        emit finished();
    });

//    ui->pushButton_2->hide();
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->pushButton_8->hide();
    ui->pushButton_9->hide();
    resize(width() - (ui->pushButton_2->width() + ui->horizontalLayout_2->margin()) * 5, height());
}

Tools::~Tools()
{
    delete ui;
}

void Tools::setTack()
{
    ui->radioBox->hide();
    ui->split1->hide();

    ui->pushButton_1->hide();
    ui->pushButton_2->hide();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    resize(144, height());
}

// add by liq 12
bool Tools::eventFilter(QObject* object, QEvent* event){
//    if(event->type() == QEvent::MouseMove){
////        QMouseEvent* mouseEvent = (QMouseEvent*)event;
//        QPoint pos = QCursor::pos();
//        QPoint topleft = mapToParent(ui->radioButton_1->geometry().topLeft());
//        QRect b1 = QRect(topleft, ui->radioButton_1->geometry().size());
////        if(object == ui->radioButton_1){
//        if(b1.contains(pos) || object == ui->radioButton_1){
//            g_toolTip->popup(QString::fromLocal8Bit("矩形"), pos);
//        }
//        QRect b2 = QRect(topleft, ui->radioButton_2->geometry().size());
//        if(b2.contains(pos) || object == ui->radioButton_2){
////            QToolTip::showText(pos, QString::fromLocal8Bit("圆形"), this);
//            g_toolTip->popup(QString::fromLocal8Bit("圆形"), pos);
//        }
//        QRect b3 = QRect(topleft, ui->radioButton_3->geometry().size());
//        if(b3.contains(pos) || object == ui->radioButton_3){
//            g_toolTip->popup(QString::fromLocal8Bit("箭头"), pos);
//        }
//        QRect b4 = QRect(topleft, ui->radioButton_4->geometry().size());
//        if(b4.contains(pos) || object == ui->radioButton_4){
//            g_toolTip->popup(QString::fromLocal8Bit("画笔"), pos);
//        }
//        QRect b5 = QRect(topleft, ui->radioButton_5->geometry().size());
//        if(b5.contains(pos) || object == ui->radioButton_5){
//            g_toolTip->popup(QString::fromLocal8Bit("马赛克"), pos);
//        }
//        QRect b6 = QRect(topleft, ui->radioButton_6->geometry().size());
//        if(b6.contains(pos) || object == ui->radioButton_6){
//            g_toolTip->popup(QString::fromLocal8Bit("文字"), pos);
//        }
//        QRect b7 = QRect(topleft, ui->radioButton_7->geometry().size());
//        if(b7.contains(pos) || object == ui->radioButton_7){
//            g_toolTip->popup(QString::fromLocal8Bit("标签"), pos);
//        }
//    }else if(QEvent::MouseButtonPress == event->type()){
////        g_toolTip->showTimer.stop();
////        g_toolTip->close();
//    }

    if(event->type() == QEvent::KeyPress){
        QKeyEvent* keyPEvent = (QKeyEvent*)event;
        if(keyPEvent->key() == Qt::Key_Escape){
//          finished可以退出但再启动死掉（待调查参照逻辑）
//            emit finished();
            // add by liq 14
            emit sigClose();
            emit sigScreenshotCancel();
            return true;
        }else if(keyPEvent->key() == Qt::Key_Shift){
            emit sigShiftKeyPressed(true);
        }
    }else if(event->type() == QEvent::KeyRelease){
        QKeyEvent* keyREvent = (QKeyEvent*)event;
        if(keyREvent->key() == Qt::Key_Shift){
            emit sigShiftKeyPressed(false);
        }
    }
    return QWidget::eventFilter(object, event);
}

//void Tools::mouseMoveEvent(QMouseEvent *event)
//{
////    QPoint pos = mapToParent(event->pos());
////    if(ui->radioButton_3->underMouse()){
////        QToolTip::showText(pos, QString::fromLocal8Bit("箭头"), this);
////    }
//}
void Tools::keyPressEvent(QKeyEvent *e) {
    if (e->key() == Qt::Key_Escape) {
        emit sigClose();
        return;
    }else if(e->key() == Qt::Key_Z){
        if(e->modifiers() & Qt::ControlModifier){
            emit sigDoUndoAction();
        }
    }else if (e->key() == Qt::Key_Y) {
        if(e->modifiers() & Qt::ControlModifier){
            emit sigDoRedoAction();
        }
    }
}

void Tools::onUpdateToolSetting(int itemType){
    switch(itemType){
        case 1:
            ui->radioButton_1->setChecked(true);
            break;
        case 2:
            ui->radioButton_2->setChecked(true);
            break;
        case 3:
            ui->radioButton_3->setChecked(true);
            break;
        case 4:
            ui->radioButton_4->setChecked(true);
            break;
        case 6:
            ui->radioButton_6->setChecked(true);
            break;
        case 7:
            ui->radioButton_7->setChecked(true);
            break;
        default:
            break;
    }
}

void Tools::onUpdateUndoBtnState(bool canUndo){
    ui->pushButton_1->setEnabled(canUndo);
    updatePushBtnStyle(ui->pushButton_1, canUndo);
}

void Tools::onUpdateRedoBtnState(bool canRedo){
    ui->pushButton_2->setEnabled(canRedo);
    updatePushBtnStyle(ui->pushButton_2, canRedo);
}

void Tools::updatePushBtnStyle(QPushButton *pushBtn, bool enabled){
    if(enabled){
        QString enableStyleSheet = QString(
            "QPushButton{"
                "border: none;"
                "min-height: 22px;"
                "min-width: 22px;"
                "font: 10pt \"FontAwesome\";"
                "border: 1px solid rgb(234,238,245);"
                "border-radius: 2px;"
                "color: rgb(80, 80, 80);"
                "background-color: rgb(234,238,245);"
            "}"
            "QPushButton:hover{"
                "border: 1px solid gray;"
                "border-radius: 2px;"
                "color: #0E90FF;"
            "}"
            "QPushButton:pressed{"
                "border: 1px solid gray;"
                "border-radius: 2px;"
                "background-color: rgb(222, 225, 232);"
                "color: #0E90FF;"
            "}"
        );
        pushBtn->setStyleSheet(enableStyleSheet);
    }else{
        QString disableStyleSheet = QString(
            "QPushButton{"
                "border: none;"
                "min-height: 22px;"
                "min-width: 22px;"
                "font: 10pt \"FontAwesome\";"
                "border: 1px solid rgb(234,238,245);"
                "border-radius: 2px;"
                "color: rgb(169, 169, 169);"
                "background-color: rgb(234,238,245);"
            "}"
            "QPushButton:hover{"
                "border: 1px solid gray;"
                "border-radius: 2px;"
                "color: #0E90FF;"
            "}"
            "QPushButton:pressed{"
                "border: 1px solid gray;"
                "border-radius: 2px;"
                "background-color: rgb(222, 225, 232);"
                "color: #0E90FF;"
            "}"
        );
        pushBtn->setStyleSheet(disableStyleSheet);
    }
}
